//
//  IdeasFeedbackViewController.m
//  IM_LearningGYDF
//
//  Created by gydf on 16/10/19.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "IdeasFeedbackViewController.h"
#import "SelectIdeaView.h"
#import "NIDropDown.h"
#import "BasicModel.h"

#define postOpinionfeedbackInfo @"postOpinionfeedbackInfo"

@interface IdeasFeedbackViewController ()<UITextViewDelegate>
{
    UITextView *_textView;//意见输入框
    NIDropDown * dropDown;//意见类型选择
    UIButton *_typeBtn;//意见类型button
    NSString *_forumId;//选择的意见类型
}
@end

@implementation IdeasFeedbackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setLeftBackNavigation];
    [self setNavigationTitle:@"意见反馈"];
    [self setRightBackNavigation];
    [self creatView];
    // Do any additional setup after loading the view.
}

#pragma mark -创建view
- (void)creatView{
    self.automaticallyAdjustsScrollViewInsets = false;
    self.view.backgroundColor=[UIColor whiteColor];
    //提示语
    UILabel *msgLabel=[MyControl creatLabelWithFrame:CGRectMake(8, 8, IPHONE_WIDTH-16, 40) text:@"        希望所有使用我们软件的同事，给出我们宝贵的意见或者建议，一旦您的建议被采纳，我们将会给出奖励!"];
    msgLabel.font=FONT_14;
    CGFloat height=[LZXHelper textHeightFromTextString:msgLabel.text width:msgLabel.width fontSize:14];
    msgLabel.height=height+5;
    [self.view addSubview:msgLabel];
    
    //意见类型
    _typeBtn=[MyControl creatButtonWithFrame:CGRectMake(15, msgLabel.y+msgLabel.height+15, IPHONE_WIDTH-30, 30) target:self sel:@selector(typeClick:) tag:110 image:nil title:@"请选择意见类型"];
    _typeBtn.layer.borderWidth=1;
    _typeBtn.layer.borderColor=[[UIColor lightGrayColor] CGColor];
    _typeBtn.layer.cornerRadius=5;
//    [_typeBtn setImage:[UIImage imageNamed:@"jiantou_down"] forState:UIControlStateNormal];
//    [_typeBtn setImage:[UIImage imageNamed:@"jiantou_up"] forState:UIControlStateSelected];
//    _typeBtn.imageEdgeInsets=UIEdgeInsetsMake(0,_typeBtn.width, 0,  0);
    [self.view addSubview:_typeBtn];
    
    //意见输入框
    _textView=[[UITextView alloc]init];
    _textView.frame=CGRectMake(15, _typeBtn.y+_typeBtn.height+15, IPHONE_WIDTH-30, 100);
    _textView.delegate=self;
    _textView.text=@"请输入您的宝贵建议!";
    _textView.textColor=[UIColor lightGrayColor];
    _textView.layer.borderWidth=1.0;
    _textView.font=[UIFont systemFontOfSize:14];
    _textView.layer.borderColor=[[UIColor lightGrayColor]CGColor];

    [self.view addSubview:_textView];
    
    //提交人
    UILabel *userName=[MyControl creatLabelWithFrame:CGRectMake(15, _textView.y+_textView.height+10, IPHONE_WIDTH-30, 21) text:[NSString stringWithFormat:@"提交人:%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"empName"]]];
    userName.textAlignment=NSTextAlignmentRight;
    userName.font=FONT_14;
    [self.view addSubview:userName];
    
    
    NSDateFormatter *formatter=[NSDateFormatter new];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateStr=[formatter stringFromDate:[NSDate date]];
    //时间
    UILabel *dateLabel=[MyControl creatLabelWithFrame:CGRectMake(15, userName.y+userName.height+10, IPHONE_WIDTH-30, 21) text:[NSString stringWithFormat:@"提交时间:%@",dateStr]];
    dateLabel.textAlignment=NSTextAlignmentRight;
    dateLabel.font=FONT_14;
    [self.view addSubview:dateLabel];
    
    //提交按钮
    UIButton *subBtn=[MyControl creatButtonWithFrame:CGRectMake(15, [dateLabel getView_Y]+[dateLabel getView_Height]+20, IPHONE_WIDTH-30, 40) target:self sel:@selector(buttonClick) tag:100 image:nil title:@"提交"];
    subBtn.backgroundColor=COLOR_MIDBLUE;
    subBtn.layer.cornerRadius=5;
    [subBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.view addSubview:subBtn];
}

#pragma mark -textViewDelegate
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    if ([_textView.text isEqualToString:@"请输入您的宝贵建议!"]) {
        _textView.textColor=[UIColor blackColor];
        _textView.text=@"";
    }
    return YES;
}

- (BOOL)textViewShouldEndEditing:(UITextView *)textView{
    if (_textView.text.length==0) {
        _textView.textColor=[UIColor lightGrayColor];
        _textView.text=@"请输入您的宝贵建议!";
    }
    return YES;
}

#pragma mark- 提交点击
- (void)buttonClick{
    [self.view endEditing:YES];
    if ([_typeBtn.titleLabel.text isEqualToString:@"请选择意见类型"]) {
        [LZXHelper postMessageWithText:@"请先选择意见类型！" View:self.view];
        return;
    }
    if (_textView.text.length<5||[_textView.text isEqualToString:@"请输入您的宝贵建议!"]) {
        [LZXHelper postMessageWithText:@"意见不能少于5个字！" View:self.view];
        return;
    }
    [self ideasTypeRequest];
}
#pragma mark - 意见类型点击
- (void)typeClick:(UIButton *)button{
    button.selected=!button.selected;
    if (button.selected) {
        //[sender setImage:[UIImage imageNamed:@"arrowUp"] forState:UIControlStateNormal];
        //显示列表
        NSArray * arr = @[@"请选择意见类型",@"功能意见",@"界面意见",@"操作意见",@"新的需求",@"其他"];
        if (dropDown == nil) {
            CGFloat f = 180;
            dropDown = [[NIDropDown alloc]showDropDown:_typeBtn :&f :arr :nil :@"down"];
            __weak NIDropDown *myDrop=dropDown;
            __weak UIButton *myTypeButton=_typeBtn;
            dropDown.myBlock=^(NSInteger chooseNum){
                [myDrop hideDropDown:myTypeButton];
                myTypeButton.selected=NO;
                [myTypeButton setTitle:arr[chooseNum] forState:UIControlStateNormal];
                dropDown=nil;
            };
//            dropDown.delegate = self;
            [self.view addSubview:dropDown];
        }else{
            [dropDown hideDropDown:_typeBtn];
            dropDown=nil;
        }
    }else{

            [dropDown hideDropDown:_typeBtn];
            dropDown=nil;
    }
}

#pragma mark -意见类型请求

- (void)ideasTypeRequest{

// 1功能意见 2界面意见 3操作意见  4新的需求   5其他
    NSArray * arr = @[@"请选择意见类型",@"功能意见",@"界面意见",@"操作意见",@"新的需求",@"其他"];
    NSArray *arr2=@[@"1",@"2",@"3",@"4",@"5"];
    NSInteger count=[arr indexOfObject:_typeBtn.titleLabel.text];
    NSDictionary *dict=@{
                         @"opinion":_textView.text,
                         @"opTypeId":arr2[count-1],
                         @"opTypeName":_typeBtn.titleLabel.text,
                         //                        @"reqStr":@"weaqtfaw"
                         };
    
    //   字典转json
    NSData* jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
    NSString* str = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    //拼接soap字符串
    NSString* soap = [NSString stringWithFormat:@"<sessionId>%@</sessionId>\n<content>%@</content>",[LZXHelper getSessionID],str];
    [LZXHelper postLoadViewWithText:HUD_PLEASEWAIT View:self.view];
    __weak IdeasFeedbackViewController *mySelf=self;
    [HttpRequest SOAPData:[LZXHelper getServerURL] SoapMthod:postOpinionfeedbackInfo soapBody:soap success:^(id responseObject) {
        [LZXHelper hideLoadViewWithView:self.view];
        
        //解析数据
        BasicModel *model=[[BasicModel alloc]initWithResponse:responseObject];
        if ([model.code isEqualToString:@"220"]) {
            NSDictionary *dict=@{@"userInfo":[model.msg copy]};
            [[NSNotificationCenter defaultCenter]postNotificationName:@"postIdeasMessage" object:nil userInfo:dict];
            [mySelf.navigationController popViewControllerAnimated:YES];
        }else if([model.code isEqualToString:@"200"]){
            [LZXHelper postMessageWithText:model.msg View:self.view];
        }
    } failure:^(NSError *error) {
        [LZXHelper hideLoadViewWithView:self.view];
    }];
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
